
import java.util.*;

import hibernate.hibernateInterface.RoomSystemHibernateInterface;
import hibernate.HibernateInitializer;
import org.hibernate.*;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.*;
import utilities.event.DPListener;
import utilities.utilities_components.PlainList;
import event.CSListener;


public class DiscountpageController extends Window{

	PlainList pList;
	
	public void showPlainList()
	{
		pList = new PlainList();
		pList.setId("DISCOUNT_LIST");
		String[] header = {"ID","服务","普通会员折扣率","高级会员折扣率","顶级会员折扣率","折扣起始时间","折扣结束时间"};
		int[] para = {0,1,2,3,4,5,6,7,8,9,10,11,12};
		pList.setHead(header);
	//	pList.setSortable(para);
		pList.setParent((Component)this);
		this.addEventListener("onNotify", new DPListener());
		pList.setListener(new PlainList.SelectListener(){
			public void onFocus(Component comp){
				if(pList.getSelectedIndex() >= 0){
					pList.getPage().setAttribute("COLDATA",pList.getColDate(pList.getSelectedIndex()));
					((Button)DiscountpageController.this.getFellow("CONDITION").getFellow("DEL")).setDisabled(false);
					((Button)DiscountpageController.this.getFellow("CONDITION").getFellow("CHANGE")).setDisabled(false);
					DiscountpageController.this.setPara();
				}else{
					pList.getPage().setAttribute("COLDATA",null);
					((Button)DiscountpageController.this.getFellow("CONDITION").getFellow("DEL")).setDisabled(true);
					((Button)DiscountpageController.this.getFellow("CONDITION").getFellow("CHANGE")).setDisabled(true);
				}
			}
		});
	}
	
	public void setPara(){
		List colData=this.getPage().getAttribute("COLDATA");
		
		Combobox box=(Combobox)this.getFellow("CONDITION").getFellow("DP_SERVICE_BOX");
		for(int i=0;i < box.getItemCount();i++)
			if(((Listcell)colData.get(1)).getLabel().equals(box.getItemAtIndex(i).getLabel())){
				box.setSelectedIndex(i);
				break;
			}
		
		Doublebox rate1=this.getFellow("CONDITION").getFellow("DP_DISCOUNT_RATE_BOX1");
		Doublebox rate2=this.getFellow("CONDITION").getFellow("DP_DISCOUNT_RATE_BOX2");
		Doublebox rate3=this.getFellow("CONDITION").getFellow("DP_DISCOUNT_RATE_BOX3");
		
		rate1.setValue(new Double(((Listcell)colData.get(2)).getLabel()));
		rate2.setValue(new Double(((Listcell)colData.get(3)).getLabel()));
		rate3.setValue(new Double(((Listcell)colData.get(4)).getLabel()));
		
		String dat1=((Listcell)colData.get(5)).getLabel();
		String dat2=((Listcell)colData.get(6)).getLabel();
		
		Datebox box1 = (Datebox)getFellow("CONDITION").getFellow("DP_DISCOUNT_TIME_FROM_BOX");
		Datebox box2 = (Datebox)getFellow("CONDITION").getFellow("DP_DISCOUNT_TIME_TO_BOX");
		
		box1.setValue(parseDate(dat1));
		box2.setValue(parseDate(dat2));
	}
	
	private Date parseDate(String str){
		int year=new Integer(str.substring(0,4))-1900;
		int month=new Integer(str.substring(5,7))-1;
		int date=new Integer(str.substring(8,10));
		return new Date(new Date(year,month,date).getTime()-new Date(0).getTime());
	}
	
	public void dateCheck()throws Exception
	{
		Datebox box1 = (Datebox)getFellow("CONDITION").getFellow("DP_DISCOUNT_TIME_FROM_BOX");
		Datebox box2 = (Datebox)getFellow("CONDITION").getFellow("DP_DISCOUNT_TIME_TO_BOX");
		if(box1.getValue() == null || box2.getValue() == null)
			throw new WrongValueException(box2,"折扣起止时间必须录入");
		if (box1.getValue() != null &&
			box2.getValue() != null &&
			box1.getValue().compareTo(box2.getValue()) >= 0)
		{
			box2.setValue(null);
			throw new WrongValueException(box2,"折扣结束日期必须晚于折扣起始日期");
		}
		this.getPage().setAttribute("timeFrom",box1.getValue());
		this.getPage().setAttribute("timeTo",box2.getValue());
	}
	
	public void getAllServices(Component comp)
	{
		this.getPage().setAttribute("INIT_BOX",comp);
		this.getPage().setAttribute("EVENT_TYPE", "flush");
		Events.sendEvent(new Event("onNotify",this));
		pList.update(this.getPage().getAttribute("UPDATE_DATA"));
	}
	
	public void rateCheck(Component comp)
	{
		if (comp.getValue() != null && comp.getValue() > 1){
			comp.setValue(null);
			throw new WrongValueException(comp,"折扣率须小于等于1");
		}
	}
	
	public void validateRates(){
		Doublebox rate1=this.getFellow("CONDITION").getFellow("DP_DISCOUNT_RATE_BOX1");
		Doublebox rate2=this.getFellow("CONDITION").getFellow("DP_DISCOUNT_RATE_BOX2");
		Doublebox rate3=this.getFellow("CONDITION").getFellow("DP_DISCOUNT_RATE_BOX3");
		rateCheck(rate1);
		rateCheck(rate2);
		rateCheck(rate3);
		if(rate1.getValue() == null && rate2.getValue() == null && rate3.getValue() == null)
			throw new WrongValueException(rate1,"必须输入折扣信息");
		this.getPage().setAttribute("rate1",rate1.getValue() == null ? new Double(1.0) : rate1.getValue());
		this.getPage().setAttribute("rate2",rate2.getValue() == null ? new Double(1.0) : rate2.getValue());
		this.getPage().setAttribute("rate3",rate3.getValue() == null ? new Double(1.0) : rate3.getValue());
	}
	
	public void addDiscount()
	{
		validateRates();
		dateCheck();
		if(((Combobox)this.getFellow("CONDITION").getFellow("DP_SERVICE_BOX")).getSelectedIndex() < 0)
			throw new WrongValueException(this.getFellow("CONDITION").getFellow("DP_SERVICE_BOX"),"请选择服务");
		this.getPage().setAttribute("service",
				((Combobox)this.getFellow("CONDITION").getFellow("DP_SERVICE_BOX")).getSelectedItem().getLabel());

		this.getPage().setAttribute("list",pList);
		this.getPage().setAttribute("EVENT_TYPE", "add");
		Events.sendEvent(new Event("onNotify",this));
	}
	
	public void deleteDiscount()
	{
		getPage().setAttribute("ID",((Listcell)((List)getPage().getAttribute("COLDATA")).get(0)).getLabel());
	
		this.getPage().setAttribute("EVENT_TYPE", "delete");
		Events.sendEvent( new Event("onNotify",this));
		pList.update(this.getPage().getAttribute("UPDATE_DATA"));
	}
	
	/*
	public void editDiscount()
	{
		Listbox box = new Listbox();
		
		PlainList list = (PlainList).getFellow("DISCOUNT_LIST");
		Listitem item = list.getlist.getSelectedIndex();
		String service = ((Listcell)item.getFirstChild()).getLabel();
		Combobox serviceBox = (Combobox)getFellow("CONDITION").getFellow("DP_SERVICE_BOX");
		for (int i = 0;i < serviceBox.getItemCount();i++)
		{
			if (serviceBox.getItemAtIndex(i).getContent().equals(service))
				serviceBox.setSelectedIndex(i);
		}
		LinkedList cellList = item.getChildren();
		((Doublebox)getFellow("CONDITION").getFellow("DP_RATE_BOX1")).setValue(((Listcell)(cellList.get(1))).getLabel());
		((Doublebox)getFellow("CONDITION").getFellow("DP_RATE_BOX2")).setValue(((Listcell)(cellList.get(2))).getLabel());
		((Doublebox)getFellow("CONDITION").getFellow("DP_RATE_BOX3")).setValue(((Listcell)(cellList.get(3))).getLabel());
		((Datebox)getFellow("CONDITION").getFellow("DP_DISCOUNT_TIME_FROM_BOX")).setValue(((Listcell)(cellList.get(4))).getLabel());
		((Datebox)getFellow("CONDITION").getFellow("DP_DISCOUNT_TIME_TO_BOX")).setValue(((Listcell)(cellList.get(5))).getLabel());
		this.getPage().setAttribute("selectIndex", ((PlainList)getFellow("DISCOUNT_LIST")).getSelectedIndex());
		this.getPage().setAttribute("EVENT_TYPE", "delete");
		Events.sendEvent( new Event("onNotify",this));
		
	}*/
	
	private void setData()
	{
		this.getPage().setAttribute("list", (PlainList)getFellow("DISCOUNT_LIST"));
		
		Combobox serviceBox = (Combobox)getFellow("DP_SERVICE_BOX");
		if (serviceBox.getSelectedItem() == null)
			throw new WrongValueException(serviceBox,"请选择服务");
		this.getPage().setAttribute("service", serviceBox.getSelectedItem().getContent());
		this.getPage().setAttribute("rate1", ((Doublebox)getFellow("CONDITION").getFellow("DP_DISCOUNT_RATE_BOX1")).getValue());
		this.getPage().setAttribute("rate2", ((Doublebox)getFellow("CONDITION").getFellow("DP_DISCOUNT_RATE_BOX2")).getValue());
		this.getPage().setAttribute("timeFrom", ((Datebox)getFellow("CONDITION").getFellow("DP_DISCOUNT_TIME_FROM_BOX3")).getValue());
		this.getPage().setAttribute("timeTo", ((Datebox)getFellow("CONDITION").getFellow("DP_DISCOUNT_TIME_TO_BOX3")).getValue());
		this.getPage().setAttribute("rate3", ((Doublebox)getFellow("CONDITION").getFellow("DP_DISCOUNT_RATE_BOX3")).getValue());
	}
}
